package com.path;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/8/5 0005.
 */
public class CreatePath {

    double [] pathIn;
    double [] pathOut = new double [0];
    ArrayList<double []> pathCreat = new ArrayList<>();
    double rowStep = 0;
    double colStep = 0;
    double step = 0.05;  //不能等于0

    int row;
    int col = 3;

    public double[] createPath(double [] pathIn) {
            double [] begin = new double[2];
            double [] end = new double[2];
            for(row=0;row < pathIn.length/col;row++){
                begin[0] = pathIn[row*col];
                begin[1] = pathIn[row*col+1];
                if (row == (pathIn.length/col-1) ) {  //最后一个点
                    end[0] = pathIn[0];
                    end[1] = pathIn[1];
                } else {
                    end[0] = pathIn[(row+1)*col];
                    end[1] = pathIn[(row+1)*col+1];
                }

                double lengthRow = end[0] - begin[0];
                double lengthCol = end[1] - begin[1];
                double length = Math.sqrt(Math.pow(lengthRow,2)+Math.pow(lengthCol,2));
                int times = (int) (length/step);
                rowStep = (end[0] - begin[0])/ length * step;
                colStep = (end[1] - begin[1])/ length * step;

                double [] add = new double[times*3];
                double [] point =new double[]{begin[0], begin[1], 0};

                int i = 0;
                while (i < times) {
                    add[i*col] = point[0];
                    add[i*col+1] = point[1];
                    add[i*col+2] = point[2];

                    pathCreat.add(point);
                    point[0] += rowStep;
                    point[1] += colStep;
                    i++;
                }

                double [] newOut = new double[pathOut.length+add.length];
                System.arraycopy(pathOut, 0, newOut, 0,                pathOut.length);
                System.arraycopy(add,      0, newOut, pathOut.length, add.length);
                pathOut = newOut;
            }


            return pathOut;
    }


}
